/***************************************************************************
 *   Copyright (C) 2008 by Philipp Müller                                  *
 *   philipp.mueller@gmx.de                                                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA          *
 ***************************************************************************/

#include <QPainter>

#include "channellistdelegate.h"
#include "channellistmodel.h"


void ChannelListDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option,
                                 const QModelIndex &index ) const
{
    QString channelName    = index.model()->data( index, ChannelListModel::ChannelNameRole ).toString();
    QString epgTitle       = index.model()->data( index, ChannelListModel::ChannelEpgTitleRole ).toString();
//     QString epgSubTitle    = index.model()->data( index, ChannelListModel::ChannelEpgSubTitleRole ).toString();
    QString epgDescription = index.model()->data( index, ChannelListModel::ChannelEpgDescriptionRole ).toString();
    QDateTime epgTime      = index.model()->data( index, ChannelListModel::ChannelEpgTimeRole ).toDateTime();
    quint32 epgDuration    = index.model()->data( index, ChannelListModel::ChannelEpgDurationRole ).toUInt();
    QString iconPath       = index.model()->data( index, ChannelListModel::ChannelIconRole ).toString();

    QString epgStart = epgTime.toString( "hh:mm" );
    QString epgEnd = epgTime.addSecs( epgDuration ).toString( "hh:mm" );
    QString epgTimeString;

    if ( epgDuration > 0 )
    {
        epgTimeString = epgStart + " - " + epgEnd;
    }

    painter->save();

    if ( option.state & QStyle::State_Selected )
        painter->fillRect( option.rect, option.palette.highlight() );

    int x = option.rect.left();
    int y = option.rect.top();
    int width = option.rect.width();
    int height = option.rect.height();

    QRect rect;

    //Icon 64x48
    painter->drawImage( x, y , QImage( iconPath ) );

    //Channel
    painter->setFont( QFont( QString(), 15 ) );

    rect = QRect( x + 70, y + 6, 200, 22 );
//     painter->drawText( x + 76, y + 24, channelName );
    painter->drawText( rect, Qt::AlignLeft | Qt::AlignTop, channelName );

    painter->setFont( QFont( QString(), 12 ) );

    // Title
    rect = QRect( x + 290, y + 6, width - 290 - 129, 25 );
    painter->drawText( rect, Qt::AlignLeft | Qt::AlignTop, epgTitle );

    // Time
    rect = QRect( width - 129, y + 6, 120, 25 );
    painter->drawText( rect, Qt::AlignRight | Qt::AlignTop, epgTimeString );

    painter->setFont( QFont( QString(), 8 ) );

    // Description
    rect = QRect( x + 70, y + 30, width - 70 - 6, height - 32 );
    painter->drawText( rect, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, epgDescription );

    painter->restore();
}

QSize ChannelListDelegate::sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const
{
    Q_UNUSED( option );
    Q_UNUSED( index );

    return QSize( 100, 48 );
}
